﻿var ShoppingMgr = {
    totalPrice: "sv.shop.total.price",
    totalNum: "sv.shop.total.num",
    shoppingList: "sv.shop.list",
    getTotalPrice: function() {
        var totalPrice = 0;
        var totalPriceVal = getStorageVal(ShoppingMgr.totalPrice);
        if (null != totalPriceVal) {
            totalPrice = Number(totalPriceVal);
        }
        return totalPrice;
    },
    getTotalNum: function() {
        var totalNum = 0;
        var totalNumVal = getStorageVal(ShoppingMgr.totalNum);
        if (null != totalNumVal) {
            totalNum = Number(totalNumVal);
        }
        return totalNum;
    },
    getShoppingList: function() {
        return getStorageVal(ShoppingMgr.shoppingList);
    },
    adjustTotalPrice: function(goodsInfo, typeOp) {
        var totalPrice = ShoppingMgr.getTotalPrice();
        totalPrice += (Number(goodsInfo.goods_info.price) * typeOp);
        if (0 > totalPrice) {
            totalPrice = 0;
        }
        setStorage(ShoppingMgr.totalPrice, totalPrice);
    },
    adjustTotalNum: function (typeOp) {
        var totalNum = ShoppingMgr.getTotalNum();
        totalNum += typeOp;
        if (0 > totalNum) {
            totalNum = 0;
        }
        setStorage(ShoppingMgr.totalNum, totalNum);
    },
    add2ShoppingList: function(goodsInfo) {
        var shoppingList = ShoppingMgr.getShoppingList();
        if (null == shoppingList) {
            shoppingList = new Array();
        }
        var bFound = false;
        for (var i = 0; i < shoppingList.length; i++) {
            if (shoppingList[i].goods_info.goodsid == goodsInfo.goods_info.goodsid) {
                bFound = true;
                shoppingList[i] = goodsInfo;
                break;
            }
        }
        if (false == bFound) {
            shoppingList.push(goodsInfo);
        }
        setStorage(ShoppingMgr.shoppingList, shoppingList);
    },
    dec2ShoppingList: function(goodsInfo) {
        var shoppingList = ShoppingMgr.getShoppingList();
        if (null != shoppingList) {
            var bFound = false;
            for (index in shoppingList) {
                if (goodsInfo.goods_info.goodsid == shoppingList[index].goods_info.goodsid) {
                    bFound = true;
                    shoppingList[index] = goodsInfo;
                    if (0 >= shoppingList[index].goods_num) {
                        shoppingList.splice(index, 1);
                    }
                    break;
                }
            }
            if (true == bFound) {
                setStorage(ShoppingMgr.shoppingList, shoppingList);
            }
        }
    },
    adjustShoppingList: function(goodsInfo, typeOp) {
        ShoppingMgr.adjustTotalPrice(goodsInfo, typeOp);
        ShoppingMgr.adjustTotalNum(typeOp);
        if (typeOp > 0) {
            ShoppingMgr.add2ShoppingList(goodsInfo);
        } else {
            ShoppingMgr.dec2ShoppingList(goodsInfo);
        }
    },
    resetShoppingMgr: function () {
        removeStorage(ShoppingMgr.totalNum);
        removeStorage(ShoppingMgr.totalPrice);
        ShoppingMgr.resetShoppingList();
    },
    resetShoppingList: function () {
        var shoppingList = ShoppingMgr.getShoppingList();
        if (null != shoppingList) {
            for (index in shoppingList) {
                var shoppingGoods = shoppingList[index];
                if (shoppingGoods.main_tab == TerminalMgr.promotionTabId) {
                    PromotionMgr.resetPromotionGoodsNum(shoppingGoods.goods_info.goodsid);
                } else {
                    GoodsMgr.resetGoodsNum(shoppingGoods.main_tab, shoppingGoods.sub_tab, shoppingGoods.goods_info.goodsid);
                }
            }
        }
        removeStorage(ShoppingMgr.shoppingList);
    },
    getShoppingGoodsNum: function (goodsId) {
        var num = 0;
        var shoppingList = ShoppingMgr.getShoppingList();
        if (null != shoppingList) {
            for (index in shoppingList) {
                if (goodsId == shoppingList[index].goods_info.goodsid) {
                    num = shoppingList[index].goods_num;
                    break;
                }
            }
        }
        return num;
    },
    adjustGoodsNum: function (goodsId, mainTab, subTab, typeOp, adjustGoodsNumShow) {
        var goodsInfo = null;
        if (mainTab == TerminalMgr.promotionTabId) {
            goodsInfo = PromotionMgr.getGoodsInfo(goodsId, typeOp);
        } else if (mainTab == TerminalMgr.vendingMachineTabId) {
            goodsInfo = GoodsMgr.getGoodsInfo(mainTab, subTab, goodsId, typeOp);
        }
        if (null != goodsInfo) {
            ShoppingMgr.adjustShoppingList(goodsInfo, typeOp);
            if (adjustGoodsNumShow) {
                adjustGoodsNumShow(goodsId, goodsInfo.goods_num, ShoppingMgr.getShoppingList());
            }
        }
    },
    getRequestGoodsList: function() {
        var goodsList = new Array();
        var shopingListInfo = ShoppingMgr.getShoppingList();
        if (null != shopingListInfo) {
            for (index in shopingListInfo) {
                var goodsInfo = {
                    "goodsid": shopingListInfo[index].goods_info.goodsid,
                    "goods_num": shopingListInfo[index].goods_num,
                    "price": shopingListInfo[index].goods_info.price
                };
                goodsList.push(goodsInfo);
            }
        }
        return goodsList;
    },
    preHandleOrder: function (orderPayType, validOrderCallback, invalidOrderCallback) {
        var totalNum = ShoppingMgr.getTotalNum();
        if (0 < totalNum) {
            var terminalInfo = TerminalMgr.getTerminalInfo();
            var sessionId = UserInfoMgr.getSessionId();
            var goodsInfo = ShoppingMgr.getRequestGoodsList();
            var requestJson = {
                "point_info": terminalInfo,
                "pay_type": orderPayType,
                "goodslist": goodsInfo,
                "session_id": sessionId
            };
            doAjax(requestJson, "order_pre_handle", function (msg) {
                if (Number(msg.result) == 0) {
                    var orderInfo = msg.order_info;
                    OrderMgr.addOrder(OrderMgr.failPayOrderListType, orderInfo);
                    OrderMgr.addOrder(OrderMgr.allOrderListType, orderInfo);
                    OrderMgr.setCurOrderInfo(orderInfo);
                    if (null != validOrderCallback) {
                        validOrderCallback(msg);
                    }
                } else {
                    OrderMgr.adjustShopingList(msg.out_stack_good);
                    if (null != invalidOrderCallback) {
                        invalidOrderCallback();
                    }
                }
            });
        }
    },
    adjustShopingList: function (outStackGoodsList) {
        var shopingList = ShoppingMgr.getShoppingList();
        var totalNum = ShoppingMgr.getTotalNum();
        var totalPrice = ShoppingMgr.getTotalPrice();
        var bChanged = false;
        for (i in outStackGoodsList) {
            for (j in shopingList) {
                if (outStackGoodsList[i].goodsid == shopingList[j].goods_info.goodsid) {
                    shopingList[j].goods_info.out_stack = GoodsMgr.isOutStack;
                    totalNum -= shopingList[j].goods_num;
                    totalPrice -= (shopingList[j].goods_num * shopingList[j].goods_info.price);
                    bChanged = true;
                    break;
                }
            }
        }
        if (true == bChanged) {
            setStorage(ShoppingMgr.totalNum, totalNum);
            setStorage(ShoppingMgr.totalPrice, totalPrice);
            if (shopingList.length > 0) {
                setStorage(ShoppingMgr.shoppingList, shopingList);
            } else {
                removeStorage(ShoppingMgr.shoppingList);
            }
        }
    },
};